package com.aynu.webshop.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aynu.webshop.web.model.Cart;
import com.aynu.webshop.web.model.Goods;
import com.aynu.webshop.web.model.Orderdetail;
import com.aynu.webshop.web.model.Orderform;
import com.aynu.webshop.web.model.User;
import com.aynu.webshop.web.pagemodel.PageModel;
import com.aynu.webshop.web.service.CartService;
import com.aynu.webshop.web.service.GoodsService;
import com.aynu.webshop.web.service.OrderdetailService;
import com.aynu.webshop.web.service.OrderformService;
import com.aynu.webshop.web.util.FinalVarUtil;

@Controller
@RequestMapping(value = "/orderform")
public class OrderformController {
	@Resource
	private OrderformService orderformService;
	@Resource
	private OrderdetailService orderdetailService;
	@Resource
	private CartService cartService;
	@Resource
	private GoodsService goodsService;
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：获取所有订单信息
	 */
	@RequestMapping(value = "/getAllOrderform", method = RequestMethod.GET)
	public ModelAndView getAllOrderform(HttpServletRequest request)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Orderform> list = orderformService.selectList();
		String th_list = FinalVarUtil.OrderformTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("orderform/orderformList");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：更新订单表状态
	 */
	@RequestMapping(value = "/updateStatus", method = RequestMethod.GET)
	public ModelAndView updateStatus(HttpServletRequest request,Integer oid,Integer status){
		ModelAndView mv = new ModelAndView("redirect:getAllOrderform");
		Orderform of = orderformService.selectById(oid);
		of.setoStatus(status);
		orderformService.update(of);
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：删除订单
	 */
	@RequestMapping(value = "/deleteOrderform", method = RequestMethod.GET)
	public ModelAndView deleteOrderform(HttpServletRequest request,Integer oid){
		ModelAndView mv = new ModelAndView("redirect:findOrderformByuid");
		orderformService.delete(oid);
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：删除订单明细
	 */
	@RequestMapping(value = "/deleteOrderformDetail", method = RequestMethod.GET)
	public ModelAndView deleteOrderformDetail(HttpServletRequest request,Integer odid){
		ModelAndView mv = new ModelAndView("redirect:myOrder");
		int oid = orderdetailService.selectById(odid).getoId();//根据明细ID获取订单id
		orderdetailService.delete(odid);//删除明细
		List<Orderdetail> orderdetail = orderdetailService.selectByOId(oid);//根基订单ID获取该订单明细
		if(orderdetail.size()==0){//如果明细为0
			orderformService.delete(oid);//删除该订单
		}
		return mv;
	}
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：查询用户订单
	 */
	@RequestMapping(value = "/findOrderformByuid", method = RequestMethod.GET)
	public ModelAndView findByuIdstatus(HttpServletRequest request,Integer uid,Integer status){
		ModelAndView mv = new ModelAndView();
		List<Orderform> lof  =null;
		if(status==null){
			 lof = orderformService.selectByuId(uid);
		}else{
			 lof = orderformService.selectByuIdstatus(uid, status);
		}
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：生成订单
	 */
	@RequestMapping(value = "/addOrderform", method = RequestMethod.POST)
	public ModelAndView addOrderform(HttpServletRequest request ,String cartItemIds,Double total){
		String[] temp=cartItemIds.split(",");
		int[] ia=new int[temp.length];
		for(int k=0;k<temp.length;k++){
		   ia[k]=Integer.parseInt(temp[k]);
		}
		ModelAndView mv = new ModelAndView("redirect:myOrder");
		User user = (User) request.getSession().getAttribute("user");
		Orderform orderform = new Orderform();//创建一个新的订单对象
		orderform.setoTime(new Date());//设置下单时间
		orderform.setoAllprice(total);//设置合计金额
		//orderform.setAdId(1);//设置收货地址（未写）
		orderform.setuId(user.getuId());//设置用户Id
		orderform.setoStatus(0);//设置状态为未付款
		orderformService.insert(orderform);//插入这个订单
		Orderdetail orderdetail=new Orderdetail();//创建一个新的订单明细
		Goods good =new Goods();//创建一个商品明细
		for(int i=0;i<ia.length;i++){
			Cart cart =cartService.selectById(ia[i]);//根据购物车id获取购物车
			good = goodsService.selectById(cart.getgId());//根据购物车的gid获取商品
			orderdetail.setoId(orderformService.findMaxOrderform());//设置订单明细的所属订单id
			orderdetail.setgId(cart.getgId());//设置订单明细的商品ID
			orderdetail.setgName(good.getgName());//设置订单明细的商品名称
			orderdetail.setgPicture(good.getgPicture());//设置订单明细的商品图片
			orderdetail.setOdCount(cart.getCarCount());//设置订单明细的商品数量
			orderdetail.setOdAllprice(cart.getCarCount()*good.getgPrice());//设置订单明细的小计
			orderdetailService.insert(orderdetail);//插入订单明细
			cartService.delete(ia[i]);//购物车删除该条数据
		}
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：支付页面
	 */
	@RequestMapping(value = "/pay", method = RequestMethod.GET)
	public ModelAndView pay(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		User user = (User) request.getSession().getAttribute("user");
		Double sum=0.0;
		List<Orderform> orderList=orderformService.selectByuIdstatus(user.getuId(), 0);
		for(int i=0;i<orderList.size();i++){
			orderList.get(i).setAdId(1);//设置收货地址
			orderformService.update(orderList.get(i));
			sum = sum+orderList.get(i).getoAllprice();
		}
		mv.addObject("sum", sum);//将订单信息展示在界面上
		mv.setViewName("orderform/pay");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：确认支付页面
	 */
	@RequestMapping(value = "/confirmpay", method = RequestMethod.GET)
	public ModelAndView confirmpay(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		User user = (User) request.getSession().getAttribute("user");
		Double sum=0.0;
		List<Orderform> orderList=orderformService.selectByuIdstatus(user.getuId(), 0);
		for(int i=0;i<orderList.size();i++){
			orderList.get(i).setoStatus(1);
			orderformService.update(orderList.get(i));
			sum = sum+orderList.get(i).getoAllprice();
		}
		mv.addObject("sum", sum);//将订单信息展示在界面上
		mv.setViewName("orderform/paySuccess");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：我的订单
	 */
	@RequestMapping(value = "/myOrder", method = RequestMethod.GET)
	public ModelAndView myOrder(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		ModelAndView mv = new ModelAndView();
		Double sum=0.0;
		List<Orderform> orderformList=orderformService.selectByuIdstatus(user.getuId(),0);//根据用户id查询该用户的所有订单
		for(int i=0;i<orderformList.size();i++){
			 sum = sum+orderformList.get(i).getoAllprice();
		}
		for(Orderform orderform2: orderformList){
			List<Orderdetail> orderdetail2 = orderdetailService.selectByOId(orderform2.getoId());//根据订单id获取订单明细
			if(orderdetail2.size()!=0){
				orderform2.setOrderdetail(orderdetail2);	
			}else{
				orderformService.delete(orderform2.getoId());
			}
		}
		request.setAttribute("orderformList", orderformList);//将订单信息展示在界面上
		request.setAttribute("sum", sum);//将订单信息展示在界面上
		mv.setViewName("orderform/confirmOrder");
		return mv;
	}
	
	
}
